﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace INSTA.MusiqueStore.UI.Web
{
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            //FillGenres();
            FillArtists();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string filepath = Server.MapPath("\\mp3//");

            System.IO.Directory.CreateDirectory(filepath +
                                              lstArtistes.SelectedItem.Text.Replace(" ", "_") + "-" +
                                               txtAlbum.Text.Replace(" ", "_"));

            HttpFileCollection uploadedFiles = Request.Files;

            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];

                try
                {
                    if (userPostedFile.ContentLength > 0)
                    {
                        if (userPostedFile.FileName.EndsWith("mp3"))
                        {

                            userPostedFile.SaveAs(filepath + lstArtistes.SelectedItem.Text.Replace(" ", "_") + "-" +
                                                  txtAlbum.Text.Replace(" ", "_") + "/" +
                                                  Path.GetFileName(userPostedFile.FileName));
                        }
                        else if(userPostedFile.FileName.EndsWith("jpg"))
                        {
                            userPostedFile.SaveAs(Server.MapPath("\\img/") + lstArtistes.SelectedItem.Text.Replace(" ", "_") + "-" +
                                                  txtAlbum.Text.Replace(" ", "_") + ".jpg");
                        }
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine("Error: <br>" + Ex.Message);
                }
            }


            // userPostedFile.SaveAs(filepath + txtArtiste.Text.Replace(" ", "_") + "-" +
            //                                           txtAlbum.Text.Replace(" ", "_") + "\\" + Path.GetFileName(userPostedFile.FileName));
            var d = decimal.Parse(txtPrix.Text);
            var id = DAL.CustomMusiqueStoreEntities.AddAlbum(txtAlbum.Text, Calendar1.SelectedDate, d, lstArtistes.SelectedItem.Text.Replace(" ", "_") + "-" +
                                   txtAlbum.Text.Replace(" ", "_") + ".jpg");
            //while (FileUpload1.HasFile)
            //{
            //    FileUpload1.SaveAs(HttpRuntime.AppDomainAppPath + "/mp3/" + txtArtiste.Text.Replace(" ", "_") + "-" +
            //                       txtAlbum.Text.Replace(" ", "_") + "/" + FileUpload1.FileName);
            //}
            Response.Redirect("~/DownloadPiste.aspx?i=" + id);


        }

        private void FillGenres()
        {
            var g = INSTA.MusiqueStore.DAL.CustomMusiqueStoreEntities.GetGenres();
            var nom = new DevExpress.Web.ASPxGridView.GridViewDataTextColumn()
            {
                FieldName = "Nom"
            };
            GvGenres.Columns.Clear();
            GvGenres.Columns.Add(nom);
            GvGenres.DataSource = g;
            GvGenres.DataBind();
        }

        private void FillArtists()
        {
            lstArtistes.DataSource = DAL.CustomMusiqueStoreEntities.GetArtists();
            lstArtistes.DataValueField = "Id";
            lstArtistes.DataTextField = "NomComplet";
            lstArtistes.DataBind();
        }

        protected void btnArtiste_Click(object sender, EventArgs e)
        {
            DAL.CustomMusiqueStoreEntities.AddArtist(txtPrenomArtiste.Text, txtNomArtiste.Text);
            FillArtists();
        }

       

        protected void Button3_Click(object sender, EventArgs e)
        {
            lblGenre.Text = lstArtistes.SelectedItem.Text;
        }




    }
}